import logging

import pytest

from resource.pom.keyboard import Keyboard
from resource.pom.settings import Settings
from utils.manager import Direction


class Test:
    app = Settings
    keyboard = Keyboard

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.进入桌面，点击设置-关于设备')
        name = device.get_param('const.product.name')
        mode = device.get_param('const.product.model')
        manufacturer = device.get_param('const.product.manufacturer')
        sn = device.get_param('ohos.boot.sn')
        soft_version = device.get_param('const.product.software.version')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.dirc_fling(Direction.Down)
        device.click(*self.app.about_device_item)
        logging.info('预期结果1.进入关于设备界面，展示信息包含：设备名称、型号、厂家信息、设备、软件版本')
        device.refresh_layout()
        device.assert_text_exist('设备名称')
        device.assert_text_exist(name)
        device.assert_text_exist('型号')
        device.assert_text_exist(mode)
        device.assert_text_exist('厂家信息')
        device.assert_text_exist(manufacturer)
        device.assert_text_exist('设备')
        device.assert_text_exist(sn)
        device.assert_text_exist('软件版本')
        device.assert_text_exist(soft_version)

        logging.info('步骤2.修改设备名称')
        device.click_element(device.get_element_by_text('设备名称'))
        device.refresh_layout()
        text_input = device.get_element_by_type('TextInput')
        device.click_element(text_input)
        device.wait(2)
        device.click(*self.keyboard.key_x)
        device.click(*self.keyboard.close)
        logging.info('预期结果2.修改设备名称生效，且会同步到蓝牙设备名称')
        device.refresh_layout()
        device.click_element(device.get_element_by_text('确定'))
        device.refresh_layout()
        device.assert_text_exist(name + 'x')
        device.go_back()
        device.dirc_fling(Direction.Up)
        device.click(*self.app.bluetooth_item)
        device.refresh_layout()
        device.assert_text_exist(name + 'x')
